package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AttentionCase;
import cn.com.css.taxInspection.attention.entity.BacklogCase;
import cn.com.css.taxInspection.attention.mapper.AttentionMapper;
import cn.com.css.taxInspection.attention.mapper.BacklogCaseMapper;
import cn.com.css.taxInspection.attention.service.AttentionService;
import cn.com.css.taxInspection.attention.service.BacklogCaseService;
import cn.com.css.taxInspection.common.exception.BusinessException;
import cn.com.css.taxInspection.common.exception.code.BaseResponseCode;
import cn.com.css.taxInspection.system.vo.res.PermissionRespNode;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Service
@Slf4j
public class BacklogCaseServiceImpl extends ServiceImpl<BacklogCaseMapper, BacklogCase> implements BacklogCaseService {
	
	@Resource
    private BacklogCaseMapper backlogCaseMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCase(BacklogCase vo) {
//        vo.setStatus(Constants.STATUS_NORMAL);
        vo.setCreateTime(new Date());
    	vo.setUpdateTime(null);
        backlogCaseMapper.insert(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCase(BacklogCase vo) {
        BacklogCase backlogCase = backlogCaseMapper.selectById(vo.getId());
        if (null == backlogCase) {
            log.error("传入 的 id:{}不合法", vo.getId());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        vo.setUpdateTime(new Date());
        backlogCaseMapper.updateById(vo);
    }
    @SuppressWarnings("unchecked")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletedCase(String id) {
        backlogCaseMapper.deleteById(id);
    }

    @Override
    public BacklogCase detailInfo(String id) {
        BacklogCase backlogCase = backlogCaseMapper.selectById(id);
        if (backlogCase == null) {
            log.error("传入 的 id:{}不合法", id);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        return backlogCase;
    }

    @Override
    public List<PermissionRespNode> selectAllCaseTree() {
        List<BacklogCase> result = backlogCaseMapper
                .selectList(Wrappers.<BacklogCase>lambdaQuery().eq(BacklogCase::getStatus,1).orderByAsc(BacklogCase::getCreateTime));
        List<PermissionRespNode> childs = new ArrayList<>();
        PermissionRespNode child;
        for (BacklogCase backlogCase:result) {
            child = new PermissionRespNode();
            child.setId(backlogCase.getId());
            child.setTitle(backlogCase.getName());
            childs.add(child);
        }

        List<PermissionRespNode> res = new ArrayList<>();
        PermissionRespNode firstLevel = new PermissionRespNode();
        firstLevel.setId("0");
        firstLevel.setTitle("积案");
        firstLevel.setSpread(true);
        firstLevel.setChildren(childs);
        res.add(firstLevel);
        return res;
    }

    @Override
    @DS("oracle")
    public Map<String, Object> getAjxxByJcajuuid(String jcajxxuuid) {
        Map<String, Object> ajxxMap = backlogCaseMapper.getAjxxMapByJcajuuid(jcajxxuuid);
        return ajxxMap;
    }
}
